import { Injectable } from '@angular/core';
import { environment } from '../../environments/env.dev';
import {ApplicantService} from "./applicant.service"

// import { NavController, NavParams } from 'ionic-angular';

// import {HttpClient} from "@angular/common/http";

import { InAppBrowser } from '@ionic-native/in-app-browser';

@Injectable()


export class UsrLogin {
  private host: string = environment.host;
  public urlLogin: string;
  timeStamp2: any;
  token: string;
  authenSign: boolean;


  constructor(public applicationService: ApplicantService,private iab:InAppBrowser) {
  }

  // ionViewDidLoad() {
  //   console.log('ionViewDidLoad WeixinPage');
  // }
  goAuth(){
    localStorage.setItem("mystateUrl", window.location.href);
    this.urlLogin = this.host + '/account/wxAuth';


    const browser = this.iab.create(this.urlLogin, '_self', {location: "yes"});
    //  do{
    //   this.MyState=localStorage.getItem("MyState")

    //  }"
    //  while(this.MyState="0")
    browser.close();
    console.log(JSON.parse(localStorage.getItem("options")))
  }


  authenJudge(){
    this.timeStamp2 = Number(localStorage.getItem("timeStamp2"));
    // console.log("this.timeStamp0");
    // console.log(this.timeStamp1);
    this.token = localStorage.getItem("token");
    // console.log("this.timeStamp0");
    // console.log(this.timeStamp1);
    console.log(this.timeStamp2!=null&&this.timeStamp2!=undefined&&this.timeStamp2!=0);

    if(this.token==null||this.token==undefined){
      this.authenSign=true;
    } else {

      if (this.timeStamp2 != null && this.timeStamp2 != undefined && this.timeStamp2 != 0) {
        this.timeStamp2 = Math.floor(((new Date().getTime()) - this.timeStamp2) / (24 * 3600 * 1000));
        if (this.timeStamp2 >= 10 || this.timeStamp2 == NaN || this.timeStamp2 < 0) {

          this.authenSign = true;
        } else {

          this.authenSign = false;
          // this.applicationService.freshTokenn();

        }

      } else {

        this.authenSign = true
      }
    }

    if(this.authenSign==true){
      console.log("this.timeStamp4")
      //   console.log(this.authenSign);
      // this.number1=Number(localStorage.getItem("aaaa"));
      // this.number1++;
      //  console.log(this.number1);
      // localStorage.setItem("aaaa",this.number1);
      //  if(this.number1<2){}
      this.goAuth();

    }
  }

}
